package com.qimo.zuoyeoa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qimo.zuoyeoa.dao.ContactMapper;
import com.qimo.zuoyeoa.entity.Contact;
import com.qimo.zuoyeoa.service.ContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ContactServiceImpl implements ContactService {

    @Autowired
    private ContactMapper contactMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean append(Contact contact) {
        return contactMapper.insert(contact) > 0;
    }

    @Override
    public PageInfo<Contact> findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        final List<Contact> contacts = contactMapper.selectAll();
        PageInfo<Contact> pageInfo = new PageInfo<>(contacts);
        return pageInfo;
    }

    @Override
    public PageInfo<Contact> findByKeyword(int pageNum, int pageSize, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        List<Contact> contacts = contactMapper.selectByKeyword(keyword);
        PageInfo<Contact> pageInfo = new PageInfo<>(contacts);
        return pageInfo;
    }

    @Override
    public Contact findById(int id) {
        return contactMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean modify(Contact contact) {
        return contactMapper.updateById(contact) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean remove(int id) {
        return contactMapper.deleteById(id) > 0;
    }
}
